package pecas.pecasclient;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ConfigActivity extends Activity {
	private static final int SELECT_FILE = 1;	
    private String selectedImagePath;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_config);
		
		
		((Button) findViewById(R.id.file)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
                intent.setType("xml/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,
                		"Select File XML Layout"), SELECT_FILE);
			}
		});
	}
	/*
	public void File_onClick(View v){
		Intent intent = new Intent();
        intent.setType("xml/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,
                "Select File XML Layout"), SELECT_FILE);
    }
	*/
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
        	Log.i("Duong", "RESULT_OK" );
            if (requestCode == SELECT_FILE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                Log.i("Duong", selectedImagePath );
                SetConfig("PathFile",selectedImagePath);
            }
        }
    }
	
	public String getPath(Uri uri) {
        // just some safety built in 
        if( uri == null ) {
            // TODO perform some logging or show user feedback
            return null;
        }
        // try to retrieve the image from the media store first
        // this will only work for images selected from gallery
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if( cursor != null ){
            int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        // this is our fallback here
        return uri.getPath();
}
	public void Save_onClick(View v){
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
		Date currentLocalTime = cal.getTime();		
		DateFormat date = new SimpleDateFormat("dd-MM-yyyy");  
		date.setTimeZone(TimeZone.getTimeZone("GMT")); 
		String localTime = date.format(currentLocalTime); 
		EditText LineID = (EditText)findViewById(R.id.txtIDline);
		EditText IPServer = (EditText)findViewById(R.id.txtIPServer);
		SetConfig("IDLine",LineID.getText().toString());
		SetConfig("IDLine",LineID.getText().toString(),localTime);
		SetConfig("IpServer",IPServer.getText().toString());
		SetConfig("IpServer",IPServer.getText().toString(),localTime);
    }	
	private void SetConfig(String Key,String Value) {		
		SharedPreferences customSharedPreference = getSharedPreferences("PecasConfig.properties", Activity.MODE_PRIVATE);
		  SharedPreferences.Editor editor = customSharedPreference.edit();
		  editor.putString(Key, Value);
		   editor.commit();
	}
	private void SetConfig(String Key,String Value,String Day) {
				
		SharedPreferences customSharedPreference = getSharedPreferences("DayConfig.properties", Activity.MODE_PRIVATE);
		  SharedPreferences.Editor editor = customSharedPreference.edit();
		  editor.putString(Key, Value);
		  editor.putString("Day", Day);
		  editor.commit();
	}
}
